/*
 * @Author: wuqingchun
 * @Date: 2024-06-22 13:54:36
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-07-10 19:56:53
 */
#ifndef __HTTP_NET_H__
#define __HTTP_NET_H__

#include <stdint.h>

#include <list>

#include "http_event.h"
#include "http_logger.h"
#include "http_poller.h"
#include "http_factory.h"

class CHTTPConnection : public IHTTPEvent
{
public:
    CHTTPConnection(CHTTPLogger& refLogger, CHTTPConnFactory& refFactory, CHTTPEventPoller& refPoller, int iSock, bool bIsListener);

    virtual int HandleIn() override;
    virtual int HandleOut() override;
    virtual void HandleError() override;
    virtual int GetEventFd() override;
    virtual int Send(const HTTP_BUF_S& refBuf) override;

public:
    void SetParser(CHTTPParser& refParser)
    {
        m_pobjParser = &refParser;
    }
    
private:
    int32_t __RecvOnce(HTTP_STRING_S& refInBuf);
    int32_t __Send(HTTP_BUF_S& refData);
    int32_t __Recv();
private:
    bool m_bIsListener{false};
    int m_iSockFd = -1;
    CHTTPLogger& m_refLogger;

    CHTTPConnFactory& m_refFactory;
    CHTTPEventPoller& m_refPoller;
    char m_acBuf[8192];
    size_t m_ulDataLen {0};

    CHTTPParser* m_pobjParser = nullptr;

    std::list<HTTP_BUF_S> m_listSndBuf; // 发送缓冲区链表
};

#endif //__HTTP_NET_H__